package net.wanho.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.wanho.constant.CommonConstant;
import net.wanho.dto.PurchaseDTO;
import net.wanho.po.BuyList;
import net.wanho.po.InStore;
import net.wanho.service.BuyListService;
import net.wanho.mapper.BuyListMapper;
import net.wanho.vo.PageInfo;
import net.wanho.vo.PurchaseVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
* @author 王洋
* @description 针对表【buy_list(采购单表)】的数据库操作Service实现
* @createDate 2024-09-03 17:43:36
*/
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@RequiredArgsConstructor
public class BuyListServiceImpl extends ServiceImpl<BuyListMapper, BuyList>
    implements BuyListService{
    private final InStoreServiceImpl inStoreService;

    @Override
    public PageInfo<PurchaseVo> purchasePageList(Integer pageNum, Integer pageSize, PurchaseDTO purchaseDTO) {
        Page<PurchaseVo> page = new Page<>(pageNum, pageSize);
        QueryWrapper<PurchaseVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(purchaseDTO.getStoreId()),"b.store_id", purchaseDTO.getStoreId());
        queryWrapper.between(ObjectUtils.isNotEmpty(purchaseDTO.getStartTime()) && ObjectUtils.isNotEmpty(purchaseDTO.getEndTime()),"b.buy_time", purchaseDTO.getStartTime(), purchaseDTO.getEndTime());
        queryWrapper.ge(!ObjectUtils.isEmpty(purchaseDTO.getStartTime()), "b.buy_time", purchaseDTO.getStartTime());
        queryWrapper.le(!ObjectUtils.isEmpty(purchaseDTO.getEndTime()), "b.buy_time", purchaseDTO.getEndTime());
        queryWrapper.like(ObjectUtils.isNotEmpty(purchaseDTO.getProductName()),"p.product_name", purchaseDTO.getProductName());
        queryWrapper.like(ObjectUtils.isNotEmpty(purchaseDTO.getBuyUser()),"b.buy_user", purchaseDTO.getBuyUser());
        queryWrapper.eq(ObjectUtils.isNotEmpty(purchaseDTO.getIsIn()),"b.is_in", purchaseDTO.getIsIn());

        this.baseMapper.selectByPage(page, queryWrapper);
        PageInfo<PurchaseVo> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page.getCurrent());
        pageInfo.setPageSize(page.getSize());
        pageInfo.setTotalNum(page.getTotal());
        pageInfo.setLimitIndex((page.getCurrent()-1) * page.getSize());
        pageInfo.setResultList(page.getRecords());

        return pageInfo;


    }

    @Override
    public void add(BuyList buyList) {
        buyList.setBuyTime(new Date());
        buyList.setIsIn(CommonConstant.NOT_IN); // 默认未入库
        this.save(buyList);
    }

    @Override
    public void inWarehouseRecordAdd(BuyList buyList) {
        /**
         * 修改采购单的状态
         */
        buyList.setIsIn(CommonConstant.IS_IN); // 已入库
        this.updateById(buyList);

        /**
         * 新增入库单
         */
        InStore inStore = new InStore();
        BeanUtils.copyProperties(buyList, inStore);
        inStore.setInNum(buyList.getFactBuyNum());
        inStore.setIsIn(CommonConstant.NOT_IN); // 默认未入库
        inStoreService.save(inStore);
    }

}




